package no.ntnu.fp.gui.notice;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import no.ntnu.fp.client.Client;
import no.ntnu.fp.gui.EventDispatcher;
import no.ntnu.fp.gui.events.NoticeReadEvent;
import no.ntnu.fp.model.DialogModel;
import no.ntnu.fp.model.Notice;
import no.ntnu.fp.model.DialogModel.StateOfDialog;
import no.ntnu.fp.model.Notice.NoticeType;

@SuppressWarnings("serial")
public class NoticePanel extends JPanel {
	private final Notice notice;
	private JButton moreInfo;
	private JCheckBox read;
	private JLabel title;
	private StateOfDialog state;

	public NoticePanel(Notice notice){
		this.notice = notice;
		setupGui();
	}	
	private void setupGui(){
		this.setBackground(Color.WHITE);
		moreInfo = new JButton(
				this.notice.getNoticeType() != NoticeType.CancelledMeeting ? 
						"Vis varsel" : "Marker som lest");
		moreInfo.addActionListener(new ShowNoticeAction());
		read = new JCheckBox("Lest");
		read.setSelected(notice.isRead());
		read.setEnabled(false);

		if (notice.isRead()) {
			moreInfo.setEnabled(false);
		}

		title = new JLabel(getTitle());
		this.setToolTipText(title.getText());

		JPanel buttons = new JPanel();
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS));
		buttons.add(read);
		buttons.add(moreInfo);

		this.add(title);
		this.add(Box.createHorizontalStrut(10));
		this.add(buttons);

	}
	private final class ShowNoticeAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			NoticeReadEvent evt = new NoticeReadEvent(this, NoticePanel.this.notice);
			EventDispatcher.dispatchEvent(evt);
			if(NoticePanel.this.notice.getNoticeType() != NoticeType.CancelledMeeting){
				DialogModel.getInstance().setNotice(notice);
				DialogModel.getInstance().setMeeting(NoticePanel.this.state,
						NoticePanel.this.notice.getMeeting());
			}
		}
	}
	private String getTitle(){
		String result = "<html>Tittel: " +  notice.getMeeting().getTitle();
		switch (notice.getNoticeType()) {
		case NewMeeting:
			result += "<br>Du er innkalt til et nytt møte";
			this.state = StateOfDialog.INVITED;
			break;
		case ChangedMeeting:
			result += "<br>Møte er endret";
			this.state = StateOfDialog.INVITED;
			break;
		case CancelledMeeting:
			result += "<br>Møte er avlyst";
			break;
		case ParticipantAccepted:
			result += "<br>En deltaker har godtatt sin møte invitasjon";
			this.state = StateOfDialog.MEETING;
			break;
		case ParticipantDeclined:
			result += "<br>En deltaker har ikke godtatt sin møte invitasjon";
			this.state = StateOfDialog.MEETING;
			break;
		}
		return result + "</html>";
	}

}
